cap log close

global path = "****"

local name= "2sls_tables"  
local date = "052620"
global logname = "$path/`name'_`date'.log"

clear
set more off
cd "$path"
log using "$logname", replace

use "$path/Data/penalty_sample.dta", clear

rename fpl_frac magi_fpl2015
replace age_2017=0 if age_2017<0

*define treatment arms
gen early = (early == 1 & spanish == 0)
gen early_spanish = (early == 1 & spanish == 1)
gen nonpersonalize = (nonpersonalize == 1 & spanish == 0)
gen nonpersonalize_spanish = (nonpersonalize == 1 & spanish == 1)
gen exemption_info = (exemption_info == 1 & spanish == 0)
gen exemption_info_spanish = (exemption_info == 1 & spanish == 1)
gen base = (base == 1 & spanish == 0)
gen base_spanish = (base == 1 & spanish == 1)

*create control variables
forval age = 40(5)50 {
gen age_`age'_64 = age_2017 < 65 & age_2017 >= `age'
}

gen log_baorhigher = log(baorhigher)

gen log_medhhinc = log(medhhinc)

gen log_pre_spanish = log(pre_spanish)

gen cov2016_0 = (covered2016 - any_dec ==0)

gen treatment_notall16 = treatment*notall16

gen treatment_age_45_64 = treatment*age_45_64

gen treatment_cov2016_0 = treatment*cov2016_0

gen covered1718_cov2016_0 = covered1718*cov2016_0

gen negative_income = (magi_fpl2015 < 0)

bysort state_num: egen state_mort2016_45 = mean(dead16) if age_45_64 == 1

local replace_missing_vals log_baorhigher log_medhhinc log_pre_spanish magi_fpl2015
					 
foreach var of varlist `replace_missing_vals'{
gen is_missing_`var' = (`var' == .)
gen `var'_corrected = 0 if `var' == . 
replace `var'_corrected = `var' if `var' != . 
}

egen fpl15cat_corrected =cut(magi_fpl2015_corrected),at(-10000000,1,1.38,2.5,4,8,10000000000)
tabulate fpl15cat_corrected, generate(income_corrected)


*Table: 2sls Robustness

estimates clear
eststo clear
*Col 1: Controls
local controls i.age_2017 male married cov2016_0 state_mort2016_45 income_corrected2-income_corrected6  ///
			   negative_income penalize2014 log_baorhigher_corrected log_medhhinc_corrected log_pre_spanish_corrected ///
			   is_missing_log_baorhigher is_missing_log_medhhinc is_missing_log_pre_spanish is_missing_magi_fpl2015 exempt2014
			   
ivregress 2sls dead1718 `controls' (covered1718=treatment) if age_45_64==1 & notall16 == 1, cl(tin) first noempty
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)&treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/2sls_robustness_`date'.ster", replace

*Col 2: Randomization Block Controls
reghdfe dead1718 (covered1718 = treatment)  if age_45_64 == 1 & notall16 == 1,  vce(cl tin) absorb(cluster1) ivsuite(ivregress) stages(first)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/2sls_robustness_`date'.ster", append

*Col 3: 40-64 year olds
ivregress 2sls dead1718 (covered1718=treatment) if age_40_64==1 & notall16 == 1, cl(tin) noempty  first
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)&treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/2sls_robustness_`date'.ster", append

*Col 4: 50-64 year olds
ivregress 2sls dead1718 (covered1718=treatment) if age_50_64==1 & notall16 == 1, cl(tin) noempty  first
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)&treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/2sls_robustness_`date'.ster", append

*Column 5: no16exclusion
ivregress 2sls dead1718 (covered1718=treatment) if age_45_64==1, cl(tin) noempty  first
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)&treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/2sls_robustness_`date'.ster", append

*Column 6: Two Instruments (treatment*notall16)
ivregress 2sls dead1718 notall16 (covered1718 = treatment treatment_notall16) if age_45_64 == 1, cl(tin) noempty  first
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)&treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/2sls_robustness_`date'.ster", append

*Column 7: Treatment Arms 
ivregress 2sls dead1718  (covered1718 = base early nonpersonalize exemption_info  ///
										base_spanish early_spanish nonpersonalize_spanish ///
										exemption_info_spanish) if age_45_64 == 1 & notall16 == 1, cl(tin) noempty first
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)&treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/2sls_robustness_`date'.ster", append




log close

